#Include ..\..\_lib\MouseEvent.ahk
#Include ..\..\_lib\ShinsOverlayClass.ahk

#Include ..\..\_lib\ctrls\Button.ahk
#Include ..\..\_lib\ctrls\Radio.ahk
#Include ..\..\_lib\ctrls\RadioGroup.ahk

#Include ToolUI\ToolUIMgr.ahk
#Include ToolUI\SubUIFree.ahk
#Include ToolUI\SubUIRect.ahk
#Include ToolUI\SubUIText.ahk

class ToolBar {

  w := 242, h := 32

  __New(x, y, mgr, cb1, cb2) {
    overlay := ShinsOverlayClass(x, y, this.w, this.h, 1, 0, 0)

    me := MouseEvent(overlay.gui.Hwnd)
    me.Register(MouseEvent.Events.LBUTTON_UP, OnLU)
    me.Register(MouseEvent.Events.LBUTTON_DOWN, OnLD)

    tooluis := ToolUiMgr()
    this.overlay := overlay, this.tooluis := tooluis

    c := '0x000000', c2 := '0x00AAAA', c3 := '0x255e90', c4 := '0x003131'
    w := 30, h := 30

    group := RadioGroup(Rect(1, 1, w * 4, h))
    _free := Radio(Rect(0, 0, w, h), c, c2, c3, 1)
    tooluis.Register('free', PenSubMenu(x, y + 2 + this.h, mgr))
    _free.OnEvent(Radio.ON_CHECKED, (*) => (mgr.UseHandler('free'), tooluis.Use('free')))
    _rect := Radio(Rect(30, 0, w, h), c, c2, c3)
    tooluis.Register('rect', RectSubMenu(x, y + 2 + this.h, mgr))
    _rect.OnEvent(Radio.ON_CHECKED, (*) => (mgr.UseHandler('rect'), tooluis.Use('rect')))
    _line := Radio(Rect(60, 0, w, h), c, c2, c3)
    tooluis.Register('line', PenSubMenu(x, y + 2 + this.h, mgr))
    _line.OnEvent(Radio.ON_CHECKED, (*) => (mgr.UseHandler('line'), tooluis.Use('line')))
    _text := Radio(Rect(90, 0, w, h), c, c2, c3)
    tooluis.Register('text', TextSubMenu(x, y + 2 + this.h, mgr))
    _text.OnEvent(Radio.ON_CHECKED, (*) => (mgr.UseHandler('text'), tooluis.Use('text')))
    group.AddRadios(_free, _rect, _line, _text)
    this.group := group

    x := 120

    box0 := Button(Rect(x += 1, 1, 30, 30), c4, c3)
    box0.OnEvent(Button.ON_RELEASE, (*) => mgr.Undo())
    box1 := Button(Rect(x += 30, 1, 30, 30), c4, c3)
    box1.OnEvent(Button.ON_RELEASE, (*) => mgr.Redo())
    box2 := Button(Rect(x += 30, 1, 30, 30), c4, c3)
    box2.OnEvent(Button.ON_RELEASE, (*) => cb1())
    box3 := Button(Rect(x += 30, 1, 30, 30), c4, c3)
    box3.OnEvent(Button.ON_RELEASE, (*) => cb2())
    this.box0 := box0, this.box1 := box1, this.box2 := box2, this.box3 := box3, this.box3 := box3

    OnLU(point, *) {
      group.OnLU(point)
      box0.OnRelease(point.InRect(box0._rect))
      box1.OnRelease(point.InRect(box1._rect))
      box2.OnRelease(point.InRect(box2._rect))
      box3.OnRelease(point.InRect(box3._rect))
      this.Refresh()
    }

    OnLD(point, *) {
      group.OnLD(point)
      if point.InRect(box0._rect)
        box0.OnPressed()
      if point.InRect(box1._rect)
        box1.OnPressed()
      if point.InRect(box2._rect)
        box2.OnPressed()
      if point.InRect(box3._rect)
        box3.OnPressed()
      this.Refresh()
    }

  }

  Destroy() {
    this.overlay.gui.Destroy()
    this.tooluis.Destroy()
  }

  Move(x, y) {
    this.overlay.gui.Move(x, y)
    this.tooluis.Move(x, y + this.h + 2)
    return this
  }

  Refresh() {
    overlay := this.overlay
    overlay.BeginDraw()
    DrawBase()

    this.group.draw(overlay)
    this.box0.Draw(overlay)
    this.box1.Draw(overlay)
    this.box2.Draw(overlay)
    this.box3.Draw(overlay)

    _x := 0
    overlay.DrawText('✎', _x += 1, 0, 12, , , 'w30 h30, aCenter aMid')
    overlay.DrawText('□', _x += 30, -2, 26, , , 'w30 h30, aCenter aMid')
    overlay.DrawText('―', _x += 30, 0, , , , 'w30 h30, aCenter aMid')
    overlay.DrawText('𝚃', _x += 30, 0, 20, , , 'w30 h30, aCenter aMid')
    overlay.DrawText('↩', _x += 30, 0, , , , 'w30 h30, aCenter aMid')
    overlay.DrawText('↪', _x += 30, 0, , , , 'w30 h30, aCenter aMid')
    overlay.DrawText('✗', _x += 30, 0, , , , 'w30 h30, aCenter aMid')
    overlay.DrawText('✓', _x += 30, 0, , , , 'w30 h30, aCenter aMid')
    _x := 120
    overlay.DrawLine(_x += 1, 0, _x, 32, '0xff01ffe1')
    overlay.DrawLine(_x += 60, 0, _x, 32, '0xff01ffe1')

    overlay.EndDraw()

    DrawBase() {
      overlay.FillRectangle(0, 0, this.w, this.h, '0xffffffff')
      overlay.DrawRectangle(1, 1, this.w - 1, this.h - 1, '0xff01ffe1')
    }
  }

}